document.addEventListener('DOMContentLoaded', function() {
    const exportButton = document.getElementById('export-job');

    if (exportButton) {
        exportButton.addEventListener('click', function() {
            // 显示加载提示
            Swal.fire({
                title: '正在生成PDF',
                text: '请稍候...',
                allowOutsideClick: false,
                allowEscapeKey: false,
                allowEnterKey: false,
                showConfirmButton: false,
                didOpen: () => {
                    Swal.showLoading();
                }
            });

            // 发起导出请求
            fetch('/export-resume/') // 确保URL与你的路由配置匹配
                .then(response => {
                    if (!response.ok) {
                        throw new Error('导出失败');
                    }
                    return response.blob();
                })
                .then(blob => {
                    // 创建下载链接
                    const url = window.URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.style.display = 'none';
                    a.href = url;
                    a.download = '个人简历.pdf';
                    document.body.appendChild(a);
                    a.click();
                    window.URL.revokeObjectURL(url);

                    // 显示成功提示
                    Swal.fire({
                        title: '导出成功',
                        text: '简历已成功导出为PDF文件',
                        icon: 'success',
                        confirmButtonText: '确定',
                        confirmButtonColor: '#1e3c72'
                    });
                })
                .catch(error => {
                    console.error('Error:', error);
                    Swal.fire({
                        title: '导出失败',
                        text: '生成PDF时发生错误，请稍后重试',
                        icon: 'error',
                        confirmButtonText: '确定',
                        confirmButtonColor: '#1e3c72'
                    });
                });
        });
    }
});